<div class="page-body">
    <div class="row">
        <div class="widget">
            <div class="widget-header
            <switch name="T_type">
                <case value="1">bg-magenta</case>
                <case value="2">bg-palegreen</case>
                <case value="3">bg-lightred</case>
                <case value="4">bg-blue</case>
                <case value="5">bg-blueberry</case>
                <case value="6">bg-gold</case>
                <default />bg-magenta
            </switch>
            ">
                <span class="widget-caption">{$T_title|default=CONTROLLER_NAME}</span>
                <!--Widget Buttons-->
            </div>
            <div class="widget-body">
                <div id="horizontal-form">
                    <form class="form-horizontal form_buy" role="form"
                          action="{:U('doBuy')}"
                          method="post" enctype="multipart/form-data" onSubmit="return false;">
                        <div class="form-group">
                            <label for="card_id" class="col-sm-3 control-label no-padding-right">会员卡：</label>
                            <div class="col-sm-3">
                                <input type="text" class="form-control" id="card_id" name="card_id" value="" onkeydown="getCard()">

                                <div id="card_info" style="display:none;">
                                </div>
                            </div>
                        </div>
                        <hr>
                        <div class="form-group   ">
                            <label class="col-sm-3 control-label no-padding-right">
                                条形码：</label>
                            <div class="col-sm-3">
                                <input type="text" id="tm" name="tm" class="form-control" value="" onkeydown="addBook()">
                            </div>
                        </div>
                        <div class="form-group   ">
                            <label class="col-sm-3 control-label no-padding-right"></label>
                            <div class="col-sm-6">
                                测试用：23132132132&nbsp;&nbsp;123625136251&nbsp;&nbsp;111111111111
                                <p class="num_book">共0本</p>
                                <table class="table table-hover" aria-describedby="simpledatatable_info">
                                    <thead class="bordered-darkorange">
                                    <tr>
                                        <th>条码</th>
                                        <th>书名</th>
                                        <th>作者</th>
                                        <th>定价/折扣价</th>
                                        <th>可购买库存</th>
                                        <th>操作</th>
                                    </tr>
                                    </thead>
                                    <tbody class="order_book_list">
                                    </tbody>
                                </table>
                            </div>
                        </div>
                        <div class="form-group   ">
                            <label class="col-sm-3 control-label no-padding-right">总金额：</label>
                            <div class="col-sm-3">
                                <input type="number" id="money" name="money" class="form-control" value="" readonly="true">
                            </div>
                            <div class="col-sm-6">
                                <span class="danger"></span>
                            </div>
                        </div>

                        <div class="form-group   ">
                            <label class="col-sm-3 control-label no-padding-right">实收：</label>
                            <div class="col-sm-3">
                                <input type="number" id="money_real" name="money_real" class="form-control" value="" onkeyup="cal_zero()" onclick="cal_zero()">
                            </div>
                            <div class="col-sm-6">
                                <span class="danger"></span>
                            </div>
                        </div>

                        <div class="form-group   ">
                            <label class="col-sm-3 control-label no-padding-right">找零：</label>
                            <div class="col-sm-3">
                                <input type="number" id="money_zero" name="money_zero" class="form-control" value="">
                            </div>
                            <div class="col-sm-6">
                                <span class="danger"></span>
                            </div>
                        </div>
                        <div class="form-group   ">
                            <label class="col-sm-3 control-label no-padding-right">支付方式：</label>
                            <div class="col-sm-3">
                                <select name="pay_way" id="pay_way">
                                    <option ></option>
                                </select>
                            </div>
                            <div class="col-sm-6">
                                <span class="danger"></span>
                            </div>
                        </div>
                        <div class="form-group  ">
                            <div class="col-sm-offset-3 col-sm-10">
                                <p class="label label-darkpink graded error_info"></p><br>
                                <input type="button" class="btn btn-primary" value="提交" onkeypress="FSubmit()" onclick="FSubmit()">
                                <input type="hidden" id="user_id" name="user_id" value="">
                                <!--图书列表-->
                                <input type="hidden" id="liststr" name="liststr" value="">
                                <!--图书原价总金额-->
                                <input type="hidden" id="money_b" name="money_b" value="">
                                <!--购书折扣-->
                                <input type="hidden" id="discount_buy" name="discount_buy" value="">
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    function getsuccess(data) {
        $("#user_id").val(data.data.user_id);
        $("#discount_buy").val(data.data.discount_buy);
        $("#card_info").show();
        $("#tm").focus();
    }
    //添加图书
    function addBook() {

        $("#tm").focus();
        var keyCode = null;

        if (event.which)
            keyCode = event.which;
        else if (event.keyCode)
            keyCode = event.keyCode;

        if (keyCode == 13) {

            var $tm = $("#tm").val();
            if ($.trim($tm) == "") {
                $("#tm").val("");
                $("#tm").focus();
                return;
            }
            $.post("{:U('Book/ob_search')}", {"tm": $tm}, function (data) {
                if (data.status == "error") {
                    alert(data.data);
                } else {
                    //调用模态框选择数据
                    getSelectBook(data);
                }
            }, "json");
        }
    }
    //模态框选择后回调
    function doadd(item) {
        $(".order_book_list tr").removeClass("active");
        $str = '<tr class="active">' +
                '<td>' +
                '<span class="book_id" style="display:none;">' + item.book_id + '</span>' +
//                '/' +
                '<span class="tm">' + item.tm + '</span></td>' +
                '<td class="sm" title="' + item.sm + '">《' + item.sm + '》</td>' +
                '<td>' + item.zyz + '</td>' +
                '<td>￥<s class="dj">' + item.dj + '</s>元&nbsp;&nbsp;/&nbsp;&nbsp;<br>￥<span class="dj_d">' + item.dj_d + '</span>元</td>' +
                '<td>' + item.stock_buy + '本</td>' +
                '<td><span class="btn btn-danger btn btn-danger action-btn" onclick="delBook($(this))">删除</span></td>' +
                '</tr>';
        $(".order_book_list").prepend($str);
        $("#tm").val("");
        $("#tm").focus();
        cal_all();
    }
    function delBook($this) {
        if (!confirm('确认删除?')) {
            return;
        }
        $this.parent().parent().remove();
        cal_all()
    }
    //计算总金额及图书list
    function cal_all() {
        var $discount = $(".discount_buy").html();
        if ($discount == "" || $discount == null) {
            $discount = 1;
        }
        var $m_total = 0;//打折总金额
        var $b_total = 0;//原总金额
        var $num_book = 0; //图书数量
        var $list = [];
        $(".order_book_list tr").each(function (i, item) {
            var $dj = item.getElementsByClassName("dj")[0].innerHTML;
            var $dj_d = parseFloat($dj * $discount).toFixed(2);
            var $book_id = item.getElementsByClassName("book_id")[0].innerHTML;
            item.getElementsByClassName("dj_d")[0].innerHTML = $dj_d;
            $m_total += parseFloat($dj_d);
            $b_total += parseFloat($dj);
            $list.push($book_id);
            $num_book++;
        });
        $("#money").val($m_total.toFixed(2));
        $("#money_b").val($b_total.toFixed(2));
        $(".num_book").html("共" + $num_book + "本");
        $("#liststr").val($list);
        cal_zero();
    }
    //计算找零
    function cal_zero() {
        var $m_total = $("#money").val();//实收
        if ($m_total == "") {
            $m_total = 0;
        }
        var $m_real = $("#money_real").val();//实收
        if ($m_real == "") {
            $m_real = 0;
        }
        var $m_zero = $m_real - $m_total;//找零
        $("#money_zero").val($m_zero.toFixed(2));
    }
    //提交
    function FSubmit() {
//        getCard_post();
//        cal_all();
        if (!confirm('确认操作?')) {
            return;
        }
        if ($("#user_id").val() == "") {
            $(".error_info").html("请输入会员卡");
            return;
        }
        if ($("#liststr").val() == "") {
            $(".error_info").html("订单图书不能为空");
            return;
        }
        if ($("#money_real").val() == "" || parseFloat($("#money_zero").val()) < 0) {
            $(".error_info").html("请输入实收金额或找零错误");
            return;
        }
        var $liststr = $("#liststr").val();
        var $user_id = $("#user_id").val();

        $.post("{:U('buyCheck')}", {"liststr": $liststr}, function (data) {
            if (data.status == "success") {
                $(".form_buy").attr("onSubmit", "");
                $(".form_buy").submit();
                return true;
            } else {
                alert(data.data);
            }
        }, "json");
    }
</script>